<?php
namespace App\Console\Commands\Task;

use App\Repositories\Task\TaskRequestRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Services\InnerApi;

use App\Repositories\Task\TaskRepository;

/**
 * 检测做单是否已经超时
 * Class TaskOver
 * @package App\Console\Commands\Task
 * @version v1.3
 */
class TaskRequestOvertime extends Command
{
	protected $signature = 'task:updateOvertimeRequest';

	protected $description = 'update overtime request record';

	private $logger;

	private $taskRequestRepository;
	private $taskRepository;

	public function __construct(TaskRequestRepository $taskRequestRepository, TaskRepository $taskRepository)
	{
		parent::__construct();
		$this->logger = Log::channel('console');
		$this->taskRequestRepository = $taskRequestRepository;
		$this->taskRepository = $taskRepository;
	}

	public function handle() {
		foreach ($this->taskRequestRepository->getOvertime() as $record) {
			try {
				$reback_num = $record->num - $record->submit_num;
				$this->taskRepository->incQuotaRemain($record->task_id, $reback_num);
				$this->taskRequestRepository->setOvertime($record->id);
				$this->info($this->signature . "readd task({$record->task_id}) quota: {$reback_num}.");
			} catch (\Exception $e) {
				$this->logger->error($this->signature, [$e->getMessage(), $record->id]);
				$this->error($e->getMessage());
			}
		}
		$this->info($this->signature . ' success！'. (new Carbon())->toDateTimeString());
	}
}